/**
 *
 *
 * @author TOTORO
 * @date 2021/8/2 19:27
 */
$(function () {
    // 点击晒图片，生成Input file
    $(".file-input").click(function () {
        var $fileInput = $("<input type='file' style='display: none' name='eImages' class='image-input'>");
        $fileInput.click();
        $fileInput.change(function () {
            // 如果上传成功，则正式的添加在DOM元素中
            var $imgLi = $("<li class='queue-file-file-164'></li>");
            var $imageBox = $("<div class='file-image-box'>");
            var $img = $("<img src=''/>");
            var $deleteImage =$("<div class='status success-status' style='display: block;'><span class='J_Img_Del_file-163 delete-btn'>删除</span></div>");
            $imageBox.append($img).append($fileInput).append($deleteImage).appendTo($imgLi);
            $(".upload-queue").append($imgLi);


            var file = $(this);
            var filePath = file.val();
            var extStart = filePath.lastIndexOf(".");
            var ext = filePath.substring(extStart, filePath.length).toUpperCase();
            if (ext != ".BMP" && ext != ".PNG" && ext != ".JPG" && ext != ".JPEG") {
                $(".ks-dialog-mask").show();
                var $g1 = gMsg("图片格式不正确！上传失败").appendTo("body");
                $g1.slideDown(300);
                return false;
            }
            // 图片大小
            var fileSize = file[0].files[0].size;
            if(fileSize > 1024 * 1024 *3){
                // 显示消息
                $(".ks-dialog-mask").show();
                var $g1 = gMsg("图片大小不能大于3MB").appendTo("body");
                $g1.slideDown(300);
                return false;
            }
            var imageLength = $(".image-input").length;
            if(imageLength == 5){
                $(".J_btn").addClass("uploader-button-disabled");
                $(".file-input").prop("disabled", true);
            }
            $(".upload-msg").text(imageLength+"/5");
            html5Reader(file,$img);
        });
    });

    // 删除图片
    $(".upload-queue").on("click", ".delete-btn", function () {
        $(this).parents(".queue-file-file-164").remove();
        var imageLength = $(".image-input").length;
        // 将上传变为可点击
        $(".J_btn").removeClass("uploader-button-disabled");
        $(".file-input").prop("disabled", false);
        $(".upload-msg").text(imageLength+"/5");
    });

    // 如果没有填写追评内容就希望能够发表评论
    $("#refer").click(function () {
        var $tea = $("#aeText");
        if($tea.val().length<=0){
            $tea.prop("placeholder", "请输入回复内容！");
            $tea.addClass("arrow_box");
            return false;
        }
        console.log(123)
        document.body.scrollTop = 0;
        $(".ks-dialog-mask").show();
        var gm = gMsg("追评成功，正在自动为您跳转...").appendTo("body");
        gm.slideDown(300);
        setTimeout(function () {
            $("#RateWriteForm").submit();
        }, 1000);
    });
    $("#aeText").focus(function () {
        $(this).removeClass("arrow_box");
    })
})


function html5Reader(file,pic) {
    var file = file[0].files[0];
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function (e) {
        pic.attr("src", this.result);
    }
}

/**
 * 提示消息
 * @param text
 * @returns {*|Window.jQuery|HTMLElement}
 */
function gMsg(text){
    return $("<div id='ks-component5005' class='ks-dialog ks-overlay cart-dialog cart-dialog-alert' tabindex='0' role='dialog' aria-labelledby='ks-stdmod-header-ks-component5005' style='width: 374px; left: 761.5px; top: 582px; position: absolute; display: none' aria-hidden='false'>\n" +
        "<a id='ks-overlay-close-ks-component5005' class='ks-dialog-close ks-overlay-close' role='button'>\n" +
        "    <span class='ks-dialog-close-x ks-overlay-close-x'>close</span>\n" +
        "</a>\n" +
        "<div id='ks-content-ks-component5005' class='ks-dialog-content ks-overlay-content'>\n" +
        "    <div class='ks-dialog-header ks-overlay-header' style='\n" +
        "' id='ks-stdmod-header-ks-component5005'>提示信息</div>\n" +
        "    <div class='ks-dialog-body ks-overlay-body' style='\n" +
        "' id='ks-stdmod-body-ks-component5005'>"+text+"</div>\n" +
        "    <div class='ks-dialog-footer ks-overlay-footer' style='\n" +
        "' id='ks-stdmod-footer-ks-component5005'><span class='cart-icon icon-warning'></span></div>\n" +
        "</div>\n" +
        "<div tabindex='0'></div></div>");
}
